using HarmonyLib;
using UnityEngine;
using System.Reflection;
using System;

namespace hatbox_hack
{
    [HarmonyPatch(typeof(Actor))]
    class ActorPatch
    {
        [HarmonyPatch(
            "Stun",
            new Type[]
            {
                typeof(float),
                typeof(Vector2),
                typeof(Vector2),
                typeof(PhysObj),
                typeof(StunSrcType),
                typeof(bool)
            }
        )]
        [HarmonyPrefix]
        public static bool Stun(
            Actor __instance,
            float len,
            Vector2 srcPos,
            Vector2 srcDir,
            PhysObj src,
            StunSrcType srcType,
            bool canBeBlocked = true
        )
        {
            if (
                __instance.CurState == ActorState.kStunned
                || srcType == StunSrcType.kSlip
                || srcType == StunSrcType.kSleepGas
                || srcType == StunSrcType.kAlwaysStun
            )
                return true;
            if (__instance.Type == ActorType.kEnemy && Contract.I.EnemyResistance)
            {
                if (!ActorExtras.I.resistance.ContainsKey(__instance))
                    ActorExtras.I.resistance[__instance] = 0;
                ActorExtras.I.resistance[__instance]++;
                if (ActorExtras.I.resistance[__instance] >= Contract.I.EnemyResistanceLevel)
                {
                    ActorExtras.I.resistance[__instance] = 0;
                    GameObjects.hatboxHack.log("Enemy Stun");
                    return true;
                }

                GameObjects.hatboxHack.log("Enemy Resist");

                if (
                    (UnityEngine.Object)FXMgr_.I.ResistanceBarPool.Prefab
                    == (UnityEngine.Object)null
                )
                    GameObjects.hatboxHack.log("NULL PREFAB!");
                ActorExtras.I.AttachResistanceBar(
                    __instance,
                    FXMgr_.I.CreateResistanceBar(__instance)
                );
                return false;
            }
            else
            {
                return true;
            }
        }

        [HarmonyPatch("MyUpdate")]
        [HarmonyPrefix]
        public static void MyUpdate(Actor __instance)
        {
            if (!__instance.gameObject.activeSelf)
            {
                // 移除出表
                ActorExtras.I.resistance.Remove(__instance);
            }
        }

        [HarmonyPatch("Awake")]
        [HarmonyPrefix]
        public static void Awake(Actor __instance)
        {
            //加入表
            ActorExtras.I.resistance.Add(__instance, 0);
        }

        [HarmonyPatch("Jump", new Type[] { typeof(float), typeof(SpriteAnimClip) })]
        [HarmonyPrefix]
        public static void JumpPatch(
            Actor __instance,
            ref float jumpForce,
            ref SpriteAnimClip jumpAnim
        )
        {
            //加入表
            if (Contract.I.EnemyBounce && __instance.Type == ActorType.kEnemy)
            {
                jumpForce *= Contract.I.EnemyBounceMultiplexer;
            }
        }
    }
}
